package com.winderinfo.yikaotianxia.ui.itembank;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.LogUtil;
import com.winderinfo.yikaotianxia.ui.collection.CollectionActivity;
import com.winderinfo.yikaotianxia.ui.mistake.MistakeActivity;
import com.winderinfo.yikaotianxia.ui.mockexam.AnswerActivity;
import com.winderinfo.yikaotianxia.ui.mockexam.GuideActivity;
import com.winderinfo.yikaotianxia.ui.record.RecordActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ItembankFragment extends BaseFragment implements Contract.View {

    @BindView(R.id.it_recycler)
    RecyclerView itRecycler;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_itembank;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        itRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        //请求免费真题数据
        /**
         * @id 专业id
         * */
        map.clear();
        map.put("proId", 1);
        presenter.onPostRequest(Api.FREETI, map, FreeBean.class);
    }

    @Override
    public void onSuccess(Object data) {
        //免费真题数据
        if (data instanceof FreeBean) {
            List<FreeBean.RowsBean> rows = ((FreeBean) data).getRows();
            FreeListAdapter freeListAdapter = new FreeListAdapter(getActivity(), rows);
            itRecycler.setAdapter(freeListAdapter);
            //接口回调
            freeListAdapter.setOnTiId(new FreeListAdapter.OnTiId() {
                @Override
                public void getId(int id) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", id);
                    LogUtil.i("ssssss", id + "");
                    openActivity(AnswerActivity.class, bundle);
                }
            });
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @OnClick({R.id.it_Mistake, R.id.it_collection, R.id.it_record, R.id.it_Presentation, R.id.it_simulation})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.it_Mistake:
                openActivity(MistakeActivity.class);
                break;
            case R.id.it_collection:
                openActivity(CollectionActivity.class);
                break;
            case R.id.it_record:
                openActivity(RecordActivity.class);
                break;
            case R.id.it_Presentation:
                break;
            case R.id.it_simulation:
                openActivity(GuideActivity.class);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

}
